package cn.linran.system.controller;

import cn.linran.basic.util.JsonResult;
import cn.linran.basic.util.PageList;
import cn.linran.system.domain.DictionaryWord;
import cn.linran.system.query.DictionaryWordQuery;
import cn.linran.system.service.IDictionaryWordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xxx
 */
@RestController
@RequestMapping("/dictionary/word")
@Api(value = "字典文字的API",description="字典文字相关的CRUD功能")
public class DictionaryWordController {

    private final IDictionaryWordService dictionaryWordService;

    public DictionaryWordController(IDictionaryWordService dictionaryWordService) {
        this.dictionaryWordService = dictionaryWordService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public DictionaryWord queryById(@PathVariable("id") Long id){

        return dictionaryWordService.loadById(id);
    }

    /**
     * 查询所有
     * @return List<Dictionary>
     */
    @GetMapping
    public List<DictionaryWord> queryAll(){
        return dictionaryWordService.loadAll();
    }

    /**
     * 高级查询
     * @param query 查询对象
     * @return PageList<Dictionary>
     */
    @PostMapping
    public PageList<DictionaryWord> queryPage(@RequestBody DictionaryWordQuery query){
        return dictionaryWordService.pageQuery(query);
    }

    /**
     * 新增or修改
     * @param dictionaryWord  实体对象
     * @return JsonResult
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody DictionaryWord dictionaryWord){
        try {
            if(dictionaryWord.getId() == null){
                dictionaryWordService.save(dictionaryWord);
            }else{
                dictionaryWordService.update(dictionaryWord);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult
                    .me()
                    .setMsg("正在准备开服前的测试！！");
        }
    }

    /**
     * 删除
     * @param id id
     * @return JsonResult
     */
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") Long id){
        try {
            dictionaryWordService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult
                    .me()
                    .setMsg("正在准备开服前的测试！！");
        }
    }
    @PatchMapping
    public JsonResult batchRemove(@RequestBody List<Long> ids){
        try {
            dictionaryWordService.batchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult
                    .me()
                    .setMsg("正在准备开服前的测试！！");
        }
    }


}
